# Python枚举类定义和使用
from enum import Enum

class Color(Enum):
    # 为序列值指定 value 值
    red = 1
    green = 2
    blue = 3

# 调用枚举成员的3种方式
print(Color.red)
print(Color["green"])
print(Color(3))
# 调取枚举成员中的 value 和 name
print(Color.red.value)
print(Color.red.name)
# 遍历枚举类中所有成员
for color in Color:
    print(color)
print()

print(Color.red == Color.green)
print(Color.red.name is Color.green.name)
#Color.red = 4 # AttributeError: Cannot reassign members.

for name, member in Color.__members__.items():
    print(name, "->", member)
print()

class Color(Enum):
    # 为序列指定 value 值
    red = 1
    green = 1
    blue = 3

print(Color.green) # Color.red
print()

'''
from enum import unique

@unique
class Color(Enum): # ValueError: duplicate values found in <enum 'Color'>: green -> red
    # 为序列指定 value 值
    red = 1
    green = 1
    blue = 3

print(Color.green)
'''

# 创建一个枚举类
Color = Enum("Color", ("red", "green", "blue"))

# 调用枚举成员的3种方式
print(Color.red)
print(Color["green"])
print(Color(3))
# 调取枚举成员中的 value 和 name
print(Color.red.value)
print(Color.red.name)
# 遍历枚举类中所有成员
for color in Color:
    print(color)
